﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;

namespace Controllers
{
    // Custom comparer for the Menus class
    class MenuComparer : IEqualityComparer<Menus>
    {
        public bool Equals(Menus x, Menus y)
        {
            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the Menus' Id are equal.
            return x.Id == y.Id;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(Menus menu)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(menu, null)) return 0;

            //Get hash code for the Name field if it is not null.
            //int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

            //Get hash code for the Code field.
            //int hashProductCode = product.Code.GetHashCode();

            //Calculate the hash code for the product.
            //return hashProductName ^ hashProductCode;
            return menu.Id.GetHashCode();
        }
    }

    [OutputCache(Location = OutputCacheLocation.None)]
    public class MenuController : BaseController
    {
        private string jsonStr = "";//循环生成的菜单字串
        private IList<Menus> menus;

        //返回所有菜单
        public ActionResult Find()
        {
            var menus = from menu in ctx.Menus select menu;
            return Json(menus);
        }

        //按角色获得菜单
        public ActionResult GetMenu()
        {
            //获得用户的所有菜单
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            IList<EmpRole> empRole =ebi.EmpRoles;
            if(empRole.Where(o=>o.RoleId==1).Count()>0)//超级管理员角色 拥有所有菜单
            {
                IQueryable<Menus> q = from menu in ctx.Menus select menu;
                menus = q.ToList();
            }
            else
            {
                List<int> empRoleIds = new List<int>();
                foreach (EmpRole er in empRole)
                {
                    empRoleIds.Add(er.RoleId);
                }
                //选择默认菜单,然后合并其他菜单
                IQueryable<Menus> q = from r in ctx.Menus where r.IsDefault==1 select r;
                menus = q.ToList();
                //按角色获取菜单
                IQueryable<Menus> menusRole = from menu in ctx.Menus
                        where (from rm in ctx.RoleMenus where empRoleIds.Contains(rm.RoleId) select rm.MenuId).Contains(menu.Id)
                        select menu;
                foreach(Menus m in menusRole)
                {
                    if(!menus.Contains(m))
                    {
                        menus.Add(m);
                    }
                }
            }
            var rootIds = menus.Where(m => m.Pid == 0).OrderBy(m=>m.OrderNo).Select(m => m);//根节点
            jsonStr = "";
            foreach (var r in rootIds)
            {
                constructJsonFromHeadNode(r);
            }
            jsonStr = "[" + jsonStr + "]";
            jsonStr = jsonStr.Replace(",]","]");
            return Content(jsonStr);
        }
        //递归调用,循环生成json树菜单
        private void constructJsonFromHeadNode(Menus menu)
        {
            //var ms = from m in menus where m.Pid == menu.Id select m;
            var ms = menus.Where(m => m.Pid == menu.Id).OrderBy(m => m.OrderNo).Select(m => m);//选择menu的下级子节点
            if(ms.Count()>0)//非叶子菜单
            {
                jsonStr += "{text:'" + menu.Name + "',iconCls:'icon-" + menu.Url + "',expanded:true,children:[";
                foreach(var m in ms)
                {
                    constructJsonFromHeadNode(m);
                }
                jsonStr += "]},";
            }
            else//叶子菜单
            {
                jsonStr += "{id:'" + menu.Url + "',text:'" + menu.Name + "',iconCls:'icon-" + menu.Url + "',leaf:true},";
            }
        }
    }
}